package com.wst.system.server.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wst.system.config.exception.GlobalExcetion;
import com.wst.system.config.job.QuarztUtils;
import com.wst.system.server.entity.SysJob;
import com.wst.system.server.entity.vo.PageSearchVo;
import com.wst.system.server.entity.vo.SysJobVo;
import com.wst.system.server.mapper.SysJobMapper;
import com.wst.system.server.service.SysJobService;
import com.wst.system.util.JSONUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class SysJobServiceImpl extends ServiceImpl<SysJobMapper, SysJob>
    implements SysJobService {

    @Override
    public IPage<SysJob> page(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<SysJob> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        IPage<SysJob> mJobIPage=this.baseMapper.selectPage(new Page(pageNum,pageSize),lambdaQueryWrapper);
        return mJobIPage;
    }

    @Override
    public SysJob getSysJobByMethodCLazz(String clazz) {
        LambdaQueryWrapper<SysJob> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysJob::getMethodclazz,clazz);
        SysJob mJob=this.baseMapper.selectOne(lambdaQueryWrapper);
        return mJob;
    }

    @Autowired
    QuarztUtils quarztUtils;

    @Override
    public SysJob saveOrUpdateSysJob(SysJobVo vo) throws GlobalExcetion {
        SysJob sysJob=new SysJob();
        BeanUtils.copyProperties(vo,sysJob);
        if(vo.getId()!=null){
            SysJob job=this.baseMapper.selectById(vo.getId());
            if(job==null){
                throw new GlobalExcetion(1,"job任务不存在");
            }
            if(!job.getTigger().equals(vo.getTigger())){
                Boolean exist=this.existTigger(vo.getTigger());
                if(exist){
                    throw new GlobalExcetion(1,"tigger已存在");
                }
            }
            if(!job.getGroupName().equals(vo.getGroupName())){
                Boolean exist=this.existGroup(vo.getGroupName());
                if(exist){
                    throw new GlobalExcetion(1,"group已存在");
                }
            }
            if(!job.getMethodclazz().equals(vo.getMethodclazz())){
                throw new GlobalExcetion(1,"定时任务执行类不能被修改");
            }
            if(!job.getMethodname().equals(vo.getMethodname())){
                throw new GlobalExcetion(1,"定时任务方法不能被修改");
            }
            this.baseMapper.updateById(sysJob);
            quarztUtils.updataQuarzt(sysJob);
        }else {
            if(vo.getParams()==null){
                JSONArray array=new JSONArray();
                sysJob.setParams(array.toJSONString());
            }else {
                try {
                    JSONArray array=JSONArray.parseArray(vo.getParams());
                }catch (Exception e){
                    throw new GlobalExcetion(1,"参数转换异常");
                }
            }
            if(vo.getTigger()!=null){
                Boolean exist=this.existTigger(vo.getTigger());
                if(exist){
                    throw new GlobalExcetion(1,"tigger已存在");
                }
            }
            if(vo.getGroupName()!=null){
                Boolean exist=this.existGroup(vo.getGroupName());
                if(exist){
                    throw new GlobalExcetion(1,"group已存在");
                }
            }
            Integer exist=this.getCount(vo.getMethodclazz(),vo.getMethodname());
            if(exist>0){
                throw new GlobalExcetion(1,vo.getMethodclazz()+"."+vo.getMethodname()+"已被使用");
            }
            sysJob.setStatus("1");
            this.baseMapper.insert(sysJob);
            quarztUtils.addQuartz(sysJob);
        }
        return sysJob;
    }

    @Override
    public IPage<SysJob> pageSysJob(PageSearchVo vo) {
        LambdaQueryWrapper<SysJob> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(JSONUtil.isNotBlank(vo.getSearch(),"jobname")){
            lambdaQueryWrapper.like(SysJob::getJobname,vo.getString("jobname"));
        }
        lambdaQueryWrapper.orderByDesc(SysJob::getCreateTime);
        IPage<SysJob> page=this.baseMapper.selectPage(new Page(vo.getPageNum(),vo.getPageSize()),lambdaQueryWrapper);
        return page;
    }

    @Override
    public boolean existTigger(String tigger) {
        LambdaQueryWrapper<SysJob> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysJob::getTigger,tigger);
        Integer count=this.baseMapper.selectCount(lambdaQueryWrapper);
        return count>0;
    }

    @Override
    public boolean existGroup(String group) {
        LambdaQueryWrapper<SysJob> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysJob::getGroupName,group);
        Integer count=this.baseMapper.selectCount(lambdaQueryWrapper);
        return count>0;
    }

    @Override
    public Integer getCount() {
        LambdaQueryWrapper<SysJob> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        return this.baseMapper.selectCount(lambdaQueryWrapper);
    }

    @Override
    public Integer getCount(String clazz, String method) {
        LambdaQueryWrapper<SysJob> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysJob::getMethodclazz,clazz);
        lambdaQueryWrapper.eq(SysJob::getMethodname,method);
        return this.baseMapper.selectCount(lambdaQueryWrapper);
    }
}




